/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.swing.JOptionPane;
/**
 *
 * @author Brian
 */
public class registration_ajax extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {

          if(request.getParameter("idajax") != null)
        {
            JavaBeans.User user = new JavaBeans.User();
            user.setId(request.getParameter("idajax"));

            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");

            response.getWriter().write("<message>");
            if(user.isValidid())
            {
                
             
            try {
                Statement st = null;
                ResultSet rs = null;
                Connection con = dataBaseController.getConnection();
                try {
                    st = (Statement) con.createStatement();
                    st = (Statement) con.createStatement();
                     String query="SELECT * FROM `brchung`.`person` WHERE id = '" + user.getId() + "'";
                    rs = (ResultSet) st.executeQuery(query);
                    rs.next();
                    rs.getString(1);
                    rs.close();
                    st.close();
                    con.close();
                    response.getWriter().write("duplicate");
                }
                catch(SQLException s)
                {
                    rs.close();
                    st.close();
                    con.close();
                    response.getWriter().write("valid");
                }}
            catch(Exception s)
            {}
            }
            else
                response.getWriter().write("invalid");
                response.getWriter().write("</message>");
        }
        

             else if (request.getParameter("password") != null)
        {
            JavaBeans.User user = new JavaBeans.User();
            user.setPassword(request.getParameter("password"));
            user.setConfirmpassword(request.getParameter("password"));


            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");

            response.getWriter().write("<message>");
            if(user.isValidPassword())
            {
                    response.getWriter().write("valid");
            }
            else
                response.getWriter().write("invalid");

            response.getWriter().write("</message>");
        }

            else if(request.getParameter("email") != null)
         {
             JavaBeans.User user = new JavaBeans.User();
            user.setEmail(request.getParameter("email"));

            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");

            response.getWriter().write("<message>");

            if(user.isValidEmail())
            {

                    response.getWriter().write("valid");
            }
            
            }
            else
                response.getWriter().write("invalid");

            response.getWriter().write("</message>");
         }
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(registration_ajax.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(registration_ajax.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
